package com.xsz.controller;

import com.xsz.entity.Resume;
import com.xsz.entity.User;
import com.xsz.service.ResumeService;
import com.xsz.util.KeyUtil;
import com.xsz.util.ResultVOUtil;
import com.xsz.util.XSZUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Date:2020/3/25 17:13
 * @Author:zzc
 * 简历
 */
@RestController
@RequestMapping("/resume")
@Api(value = "简历测试类")
public class ResumeController {

    @Resource
    ResumeService resumeService;

    @Resource
    XSZUtil xszUtil;

    /**查询当前用户所有简历**/
    @GetMapping("/showAll")
    public ResultVO showById(HttpServletRequest request){
        User loginUser = (User)request.getSession().getAttribute("loginuser");

        return ResultVOUtil.success(resumeService.showById(loginUser.getId()));
    }

    /**添加简历**/
    @PostMapping("addResume")
    public ResultVO addResume(@RequestBody Resume resume, HttpServletRequest request){
        return ResultVOUtil.success(resumeService.addResume(resume, (User) request.getSession().getAttribute("loginuser")));
    }

    /**修改当前用户简历**/
    @PostMapping("editResume")
    public ResultVO editResume(@RequestBody Resume resume, HttpServletRequest request){
        resume.setCreateBy(((User) request.getSession().getAttribute("loginuser")).getId());
        return ResultVOUtil.success(resumeService.editResume(resume));
    }

    /**删除简历**/
    @PostMapping("deleteResume/{id}")
    public ResultVO deleteResume(@PathVariable("id") String id){
        resumeService.deleteResume(id);
        Map<String, String> result = new HashMap<>();
        result.put("result", "成功");
        return ResultVOUtil.success(result);
    }



    //根据用户id查询简历
    @ApiOperation(value="根据用户id查询该用户的简历")
    @GetMapping("getResumeByUid")
    public ResultVO getResumeByUid(HttpServletRequest req ) {
        String id=((User)req.getSession().getAttribute("loginuser")).getId();
        return ResultVOUtil.success(resumeService.selectById(id));
    }

    //根据简历id查询简历
    @ApiOperation(value="根据简历id查询简历")
    @GetMapping("getResumeByResumeId/{id}")
    public ResultVO getResumeByResumeId(@PathVariable String id ) {

        return ResultVOUtil.success(resumeService.selectByResumeId(id));
    }


    @Value("${fileUpLoadPath}")
    String filePath;

    /**
     * @RequestMapping 表示同时支持接收post和get请求。
     * 如果加上method=RequestMethod.POST， 表示只接收post请求
     * 其实有一个注解可以直接指定，只接收post请求---@PostMapping
     * @param req
     * @param multiReq
     * @throws IOException
     */
    @PostMapping("/newResume")
    // @RequestMapping(value="/testUpload3",method=RequestMethod.POST)
    public void newResume(HttpServletRequest req, MultipartHttpServletRequest multiReq, HttpServletResponse response) throws IOException{
        MultipartFile multipartFile= multiReq.getFile("file-input");
        Resume resume=new Resume();
        if(multipartFile!=null&&!"".equals(multipartFile.getOriginalFilename())){
            String filename=multipartFile.getOriginalFilename();
            System.out.println("文件名字："+multipartFile.getOriginalFilename());
            File file=new File(filePath+filename);
            multipartFile.transferTo(file);
            resume.setImage1("/job/word/"+filename);
        }

        resume.setResumename(req.getParameter("resumename1"));
        resume.setName(req.getParameter("name1"));
        resume.setSex(req.getParameter("sex1"));
        resume.setNation(req.getParameter("nation1"));
        resume.setEmailaddress(req.getParameter("emailaddress1"));
        resume.setMobile(req.getParameter("mobile1"));
        resume.setMajor(req.getParameter("major1"));
        resume.setEducation(req.getParameter("education1"));
        resume.setBorn(req.getParameter("born1"));
        resume.setAddress(req.getParameter("address1"));
        resume.setWorkedyears(req.getParameter("workedyears1"));
        resume.setCertificate(req.getParameter("certificate1"));
        resume.setTrainExp(req.getParameter("trainExp1"));
        resume.setAssessment(req.getParameter("assessment1"));
        resume.setProjectExp(req.getParameter("projectExp1"));
        resume.setId(KeyUtil.getId());
        System.out.println(resume.getAddress());
        String uid=((User)req.getSession().getAttribute("loginuser")).getId();
        resume.setCreateBy(uid);
        System.out.println("简历对象："+resume);
        Map<String, Object> map =XSZUtil.objectToMap(resume);
        ResponseEntity<ResultVO> resultVO=xszUtil.callService("http://DOC/job/resume",map);
        if("成功".equals(resultVO.getBody().getMsg())){
            LinkedHashMap<String, Object> rs=(LinkedHashMap)resultVO.getBody().getData();
            String path=(String) rs.get("filePath");
            path=path.substring(path.lastIndexOf(File.separator)+1);
            resume.setWordPath("/job/word/"+path);
        }
        resumeService.addResume(resume);
        response.sendRedirect("/job/resumeList.html");

    }




}
